package com.kkm.controller;

import com.kkm.pojo.Admin;
import com.kkm.pojo.PageBean;
import com.kkm.pojo.Type;
import com.kkm.service.AdminService;
import com.kkm.service.Imp.AdminServiceImp;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/admin")
public class AdminServlet extends BaseServlet {
    private AdminService service=new AdminServiceImp();

    /*
        登录验证
    */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String password = req.getParameter("password");
        String phone =req.getParameter("phone");
        System.out.println(phone+" "+password+"11111");
        Admin admin = new Admin();
        admin.setPhone(phone);
        admin.setPassword(password);

        //调用业务层
        admin= service.login(admin);
//        System.out.println(loginConsumer.getUsername());
        if (admin != null) {

            HttpSession session = req.getSession();

            //登录成功
            //将用户存入session
            session.setAttribute("Admin", admin);
            //跳转页面
//            resp.sendRedirect(req.getContextPath() + "/index.jsp");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        } else {
            //登录失败
            //提示信息
            req.setAttribute("/flag", "用户名或密码错误！");
            //跳转登录页面
            req.getRequestDispatcher("/adminLogin.jsp").forward(req, resp);
        }
    }

    /*
     退出当前登录
     */
    public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求的编码格式
        req.setCharacterEncoding("UTF-8");
//        销毁session
        req.getSession().invalidate();
//        跳转登录界面
        req.setAttribute("/flag", "成功退出登录！");
        req.getRequestDispatcher("/adminLogin.jsp").forward(req, resp);
        }
        
        //修改管理员密码第一步 changePass
        public void checkAdminById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String aid = req.getParameter("aid");
            Admin admin=service.checkAdminById(aid);
            req.setAttribute("Admin",admin);
            req.getRequestDispatcher("/changePass.jsp").forward(req,resp);
        }


        public void changePass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String aid = req.getParameter("id");
            Map<String, String[]> condition = req.getParameterMap();
            for (String key :condition.keySet()){
                System.out.println(key+"="+condition.get(key)[0]);
            }
//            if(req.getParameter("password")==null){
//
//            }else {
//
//            }
            service.changePass(condition,aid);

            req.getSession().invalidate();
//        跳转登录界面
            req.setAttribute("/flag", "成功退出登录！");
            req.getRequestDispatcher("/adminLogin.jsp").forward(req, resp);
//            req.getRequestDispatcher("/changePass.jsp").forward(req, resp);
        }

        }

